package com.hongdee.crm.tag.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.crm.Constant;
import com.hongdee.crm.tag.endtity.TCrmCarTag;
import com.hongdee.crm.tag.service.ITcrmCarTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/5/10.
 */
@RestController
@RequestMapping("/crm/tag")
public class TCrmCarTagController {

    @Autowired
    private ITcrmCarTagService tcrmCarTagService;

    /**
     * 获得标签树信息
     * @param uid
     * @param token
     * @param tagCode
     * @return
     * @throws Exception
     */
    @RequestMapping( "/leftTree")
    @ResponseBody
    public ResultEntity leftTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "tagCode", defaultValue = "-1") String tagCode) throws Exception {
        ResultEntity res=new ResultEntity();
        List<Map<String, Object>> leftTree = tcrmCarTagService.getLeftTree(tagCode, uid);
        res.setData(leftTree);
        res.setSuccess(true);
        res.setMsg("查询");
        return res;
    }

    /**
     * 根据父级编码 获得父级下标签信息 分页显示
     * @param uid
     * @param token
     * @param tagCode
     * @param start
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "tagCode", defaultValue = "-1") String tagCode,
                             @RequestParam(name = "start", defaultValue = "0") String start,
                             @RequestParam(name = "limit", defaultValue = "50") String limit) throws Exception {
        Map map=tcrmCarTagService.page(start,limit,tagCode,uid);
        map.put("success",true);
        return map;
    }

    /**
     * 根据标签ID 获得标签详情
     * @param uid
     * @param token
     * @param id
     * @return
     */
    @RequestMapping("/info")
    @ResponseBody
    public Map info(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        Map map=tcrmCarTagService.info(id,uid);
        map.put("success",true);
        return map;
    }

    /**
     * 根据标签ID启用
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/start")
    @ResponseBody
    public Map start(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        Map res=new HashMap();
        int i=tcrmCarTagService.disAndEnableTag(id, String.valueOf(Constant.DEF_STATUS.START.ordinal()),uid);
        res.put("success",(i>=0)?true:false);
        return res;
    }

    /**
     * 根据标签ID停用
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/stop")
    @ResponseBody
    public Map stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        Map res=new HashMap();
        int i=tcrmCarTagService.disAndEnableTag(id, String.valueOf(Constant.DEF_STATUS.STOP.ordinal()),uid);
        res.put("success",(i>=0)?true:false);
        return res;
    }

    /**
     * 新增、编辑接口
     * @param uid
     * @param token
     * @param tag
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public Map save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TCrmCarTag tag) throws Exception {
        Map res=new HashMap();
        tag=tcrmCarTagService.saveOrUpdate(tag,uid);
        res.put("data",tag);
        res.put("success",true);
        return res;
    }

}
